var id = $("#examId").val();
if(null==id||""==id){
	window.location.href="stu_index.jsp";
}else{
	$.ajax({
		url:'student/attendExam',
		dataType:'json',
		type:'post',
		data:{eId:id},
		success:function(data){
			if(null==data){
				alert("开考时间已超过30分钟，禁止进入考试!");
				window.location.href="stu_index.jsp";
			}
			
			//加载答卷答案
			var stuAnswer = {};
			$.ajax({
				url:'stuAnswer/loadAnswer',
				type:'post',
				dataType:'json',
				data:{eId:id},
				success:function(answer){
					stuAnswer = answer;
					
					var paper = data.paper;
					$('.paper-header').find('h2').text(paper.title);
					$('.paper-header').find('h4').text(paper.description+'(考试时长:'+paper.time+'min)');
					var content = $('#paper-content');
					var html = "";
					for(var i=0;i<paper.topics.length;i++){
						html += '<li class="list-group-item"><h4><strong>'+paper.topics[i]._index+'.'+paper.topics[i].title+'</strong></h4></li>';
						var subtopics = paper.topics[i].subtopics;
						for(var j=0;j<subtopics.length;j++){
							
							//获取到对应题目的学生答案
							var stuOption = {};
							
							for(var l=0;l<stuAnswer.stuOptions.length;l++){
								if(subtopics[j].id==stuAnswer.stuOptions[l].subtopics.id){
									stuOption = stuAnswer.stuOptions[l].options;
								}
							}
							console.log(JSON.stringify(stuOption));
							if(subtopics[j].single!=null){
								html += '<li class="list-group-item">'+
									'<h4>'+subtopics[j]._index+'.'+subtopics[j].single.title+'</h4>'+
									'<ul class="row list-style-none subtopic-item">'+
									'<input class="subtopics-id" type="hidden" value="'+subtopics[j].single.id+'">'+
									'<input class="subtopics-type" type="hidden" value="Single">';
								
								for(var k=0;k<subtopics[j].single.options.length;k++){
									var option = subtopics[j].single.options[k];
									html += '<li class="col-sm-6">'+
												'<label class="radio-inline">';
									if(stuOption.length>0&&option.id==stuOption[0].id)
										html += '<input checked type="radio" name="radio'+subtopics[j]._index+'" id="" value="'+option.id+'" onchange="radioChange(this)">'+option.content;
									else
										html += '<input type="radio" name="radio'+subtopics[j]._index+'" id="" value="'+option.id+'" onchange="radioChange(this)">'+option.content;
									
									html += '</label>'+
										'</li>';
								}
								html += '</ul>';
								html += '</li>';
							}else if(subtopics[j].multiple!=null){
								html += '<li class="list-group-item">'+
								'<h4>'+subtopics[j]._index+'.'+subtopics[j].multiple.title+'</h4>'+
								'<ul class="row list-style-none subtopic-item">'+
								'<input class="subtopics-id" type="hidden" value="'+subtopics[j].single.id+'">'+
								'<input class="subtopics-type" type="hidden" value="Multiple">';
								for(var k=0;k<subtopics[j].multiple.options.length;k++){
									var option = subtopics[j].multiple.options[k];
									html += '<li class="col-sm-6">'+
												'<label class="checkbox-inline">';
									
									var flag = false;
									for(var h=0;h<stuOption.length;h++){
										if(option.id==stuOption[h].id)flag=true;
									}
									
									if(flag)
										html += '<input checked type="checkbox" name="checkbox'+subtopics[j]._index+'" id="" value="'+option.id+'">'+option.content;
									else
										html += '<input type="checkbox" name="checkbox'+subtopics[j]._index+'" id="" value="'+option.id+'">'+option.content;
												
									html += '</label>'+
										'</li>';
								}
								html += '</ul>';
								html += '</li>';
							}else if(subtopics[j].judgment!=null){
								html += '<li class="list-group-item">'+
								'<h4>'+subtopics[j]._index+'.'+subtopics[j].judgment.title+'</h4>'+
								'<ul class="row list-style-none subtopic-item">'+
								'<input class="subtopics-id" type="hidden" value="'+subtopics[j].judgment.id+'">'+
								'<input class="subtopics-type" type="hidden" value="Judgment">';
								
								for(var k=0;k<subtopics[j].judgment.options.length;k++){
									var option = subtopics[j].multiple.options[k];
									if(option.content == 'true'){
										html += '<li class="col-sm-6">'+'<label class="radio-inline">';
										if(stuOption.length>0&&option.id==stuOption[0].id)
											html += '<input checked type="radio" name="radio'+subtopics[j]._index+'" id="" value="true">正确</label>';
										else
											html += '<input type="radio" name="radio'+subtopics[j]._index+'" id="" value="true">正确</label>';
											html += '</li>';
									}else{
										html += '<li class="col-sm-6">'+'<label class="radio-inline">';
										if(stuOption.length>0&&option.id==stuOption[0].id)
											html += '<input checked type="radio" name="radio'+subtopics[j]._index+'" id="" value="false">错误</label>';
										else
											html += '<input type="radio" name="radio'+subtopics[j]._index+'" id="" value="false">错误</label>';
											html += '</li>';
									}
								}

								html += '</ul>';
								html += '</li>';
							}else if(subtopics[j].explanation!=null){
								html += '<li class="list-group-item">'+
								'<h4>'+subtopics[j]._index+'.'+subtopics[j].explanation.title+'</h4>';
								
								html += '<li class="list-group-item  subtopic-item">'+
									'<input class="subtopics-id" type="hidden" value="'+subtopics[j].explanation.id+'">'+
									'<input class="subtopics-type" type="hidden" value="Explanation">';
								if(stuOption.length>0&&stuOption.option.length>0&&stuOption.option[0].content!=null){
									html += '<textarea class="form-control answer-textarea" rows=6>'+stuOption.option[0].content+'</textarea>';
								}else{
									html += '<textarea class="form-control answer-textarea" rows=6></textarea>';
								}
								html += '</li>';
								
								html += '</li>';
							}else{
								
							}
							
						}
					}
					$("#paper-content").html(html);
				},
				error:function(){
					alert("出错!");
				}
			});
			
		},
		error:function(){
			alert("出错!");
		}
	});
}
/*选择答案事件*/
function radioChange(obj){
	alert($(obj).val());
}
